﻿Imports DTO
Imports BUS
Imports System.ComponentModel
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraBars
Imports DevExpress.XtraPrinting.Preview
Imports System.Threading

Public Class ucPhieuBanHang

    Private dsKH As List(Of KhachHangDTO)
    Private _load As Boolean = False
    Private maPhieu As String
    Private dsNhanVien As DataTable
    Private dsDieuKhoan As List(Of DieuKhoanTTDTO)
    Private hinhThucTT As List(Of HinhThucTTDTO)
    Private dsKhoHang As DataTable
    Private dsChiTiet As BindingList(Of ChiTietBanHangDTO)
    Private dsHangHoa As List(Of HangHoaBanHangDTO)
    Private dtChiTietHH As DataTable
    Private chiecKhau As Double
    Private tienChiecKhau As Double
    Private tienThueVat As Double
    Private phieuBH As PBanHangDTO
    Private pq As New PhanQuyenNhonNDDTO
    Private xuatBaoCao As Thread
    Private maDH As String

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub


    Private Sub ucPhieuBanHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        pq = ChucNangBUS.layQuyenCuaND(frmMain.maND, 31)
        SplashScreenManager2.ShowWaitForm()
        loadDSKH()
        loadMaPhieu()
        loadDSNhanVien()
        loadDieuKhoanTT()
        LoadHinhThuc()
        loadThongTinNgay()
        loadDSHangHoa()
        loadDSKhoHang()
        addDatasourceDataGrid()

        cEditCkPT.EditValue = chiecKhau
        cEditVATPT.EditValue = 0
        tinhTongDonHang()

        _load = True

        cbbTenKH.EditValue = dsKH(0).MaKhachHang
        cbbMaKh.EditValue = dsKH(0).MaKhachHang

        SplashScreenManager2.CloseWaitForm()

        'RepositoryItemCalcEdit1.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
        'RepositoryItemCalcEdit1.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        'RepositoryItemCalcEdit1.DisplayFormat.FormatString = "###,###,###.##"
        'RepositoryItemCalcEdit1.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        'RepositoryItemCalcEdit1.EditFormat.FormatString = "###,###,###.##"

        RepositoryItemCalcEdit1.Mask.UseMaskAsDisplayFormat = True
        RepositoryItemCalcEdit1.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        RepositoryItemCalcEdit1.Mask.EditMask = "###,###,###"

        calcCk.Mask.UseMaskAsDisplayFormat = True
        calcCk.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        calcCk.Mask.EditMask = "f"

        gridPhieuHang.OptionsView.ColumnAutoWidth = True
        gridPhieuHang.ScrollStyle = ScrollStyleFlags.LiveHorzScroll
        gridPhieuHang.HorzScrollVisibility = ScrollVisibility.Always
        gridPhieuHang.AddNewRow()

        ckbIn.Checked = True


    End Sub

    Private Sub loadDSKH()
        dsKH = KhachHangBUS.layDanhSachKHTenVaMa()
        cbbTenKH.Properties.DataSource = dsKH
        cbbTenKH.Properties.DisplayMember = "TenKhachHang"
        cbbTenKH.Properties.ValueMember = "MaKhachHang"

        cbbMaKh.Properties.DataSource = dsKH
        cbbMaKh.Properties.DisplayMember = "MaKhachHang"
        cbbMaKh.Properties.ValueMember = "MaKhachHang"

    End Sub

    Private Sub cbbTenKH_EditValueChanged(sender As Object, e As EventArgs) Handles cbbTenKH.EditValueChanged, cbbMaKh.EditValueChanged
        If _load = True Then
            Dim control As DevExpress.XtraEditors.GridLookUpEdit = DirectCast(sender, DevExpress.XtraEditors.GridLookUpEdit)
            Dim makh As String = ""
            makh = control.EditValue.ToString
            cbbTenKH.EditValue = makh
            cbbMaKh.EditValue = makh
            Dim kh As KhachHangDTO = dsKH.Find(Function(khachHang As KhachHangDTO)
                                                   Return khachHang.MaKhachHang = makh
                                               End Function)
            txtDt.Text = kh.DienThoai
            txtDiaChi.Text = kh.DiaChi
            chiecKhau = kh.ChiecKhau
            cEditCkPT.EditValue = chiecKhau
        End If
    End Sub

    Private Sub cbbTenKH_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbbTenKH.Properties.ButtonClick
        If e.Button.Tag.ToString = "cbTaoKH" Then
            cbbTenKH.Hide()
            Dim frm As New frmThemKhachHang
            frm.ShowDialog()
        Else
            cbbTenKH.ShowPopup()
        End If
    End Sub

    Private Sub loadMaPhieu()
        maPhieu = PhieuBanHangBUS.layMaPhieu()
        txtPhieu.Text = maPhieu
    End Sub

    Private Sub loadDSNhanVien()
        dsNhanVien = NhanVienBUS.LayDSNhanVienCBX()
        cbbNhanVienLap.Properties.DataSource = dsNhanVien
        cbbNhanVienLap.Properties.DisplayMember = "TenNhanVien"
        cbbNhanVienLap.Properties.ValueMember = "MaNhanVien"
        cbbNhanVienLap.EditValue = dsNhanVien(0)("MaNhanVien").ToString
    End Sub

    Private Sub loadDieuKhoanTT()
        dsDieuKhoan = DieuKhoanTTBUS.layDSDieuKhoan()
        cbDieuKienTT.Properties.DataSource = dsDieuKhoan
        cbDieuKienTT.Properties.ValueMember = "MaDieuKhoan"
        cbDieuKienTT.Properties.DisplayMember = "TenDieuKhoan"
        cbDieuKienTT.EditValue = 1
    End Sub

    Private Sub LoadHinhThuc()
        hinhThucTT = HinhThucTTBUS.layDsHinhThucTT()
        cbbThanhToan.Properties.DataSource = hinhThucTT
        cbbThanhToan.Properties.DisplayMember = "TenThanhToan"
        cbbThanhToan.Properties.ValueMember = "MaThanhToan"
        cbbThanhToan.EditValue = hinhThucTT(0).MaThanhToan
    End Sub

    Private Sub loadThongTinNgay()
        Dim dateNow As Date = Date.Now
        dateLapPhieu.EditValue = dateNow
        dateGiao.EditValue = dateNow
        dateThanhToan.EditValue = dateNow
    End Sub

    Private Sub loadDSKhoHang()
        dsKhoHang = KhoHangBUS.LoadBangKhoHang()
        cbxKhoHang.DataSource = dsKhoHang
        cbxKhoHang.ValueMember = "MaKhoHang"
        cbxKhoHang.DisplayMember = "TenKhoHang"
    End Sub

    Private Sub addDatasourceDataGrid()
        dtChiTietHH = New DataTable()
        dtChiTietHH.Columns.Add("MaHang")
        dtChiTietHH.Columns.Add("TenHang")
        dtChiTietHH.Columns.Add("MaKho")
        dtChiTietHH.Columns.Add("DonVi")
        dtChiTietHH.Columns.Add("SoLuong")
        dtChiTietHH.Columns.Add("DonGia")
        dtChiTietHH.Columns.Add("ThanhTien")
        dtChiTietHH.Columns.Add("ck")
        dtChiTietHH.Columns.Add("ChietKhau")
        dtChiTietHH.Columns.Add("ThanhToan")
        grdCtrlPhieuBan.DataSource = dtChiTietHH
    End Sub

    Private Sub loadDSHangHoa()
        dsHangHoa = HangHoaBUS.layDSHangHoaBanHang()
        gridMaHangHoa.DataSource = dsHangHoa
        gridMaHangHoa.DisplayMember = "MaHangHoa"
        gridMaHangHoa.ValueMember = "MaHangHoa"

        gridTenHangHoa.DataSource = dsHangHoa
        gridTenHangHoa.DisplayMember = "TenHangHoa"
        gridTenHangHoa.ValueMember = "MaHangHoa"

        'gridTenHangHoa.View.ColumnAutoWidth()
        'gridTenHangHoa.BestFitColumns()

    End Sub

    'Private Sub addColumInGridLockup(grid As GridLockup)

    Private Sub tinhTien(index As Integer, hh As HangHoaBanHangDTO)
        gridPhieuHang.SetRowCellValue(index, colMaHang, hh.MaHangHoa)
        gridPhieuHang.SetRowCellValue(index, colTenHang, hh.MaHangHoa)
        gridPhieuHang.SetRowCellValue(index, colDonVi, hh.TenDonVi)
        gridPhieuHang.SetRowCellValue(index, colKhoHang, hh.MaKhoHang)
        Dim soluong As Integer = Integer.Parse(gridPhieuHang.GetRowCellValue(index, colSoLuong).ToString)
        Dim donGia As Double = Double.Parse(gridPhieuHang.GetRowCellValue(index, colDonGia).ToString)
        Dim ThanhTien As Double = soluong * donGia
        gridPhieuHang.SetRowCellValue(index, colThanhTien, ThanhTien)

        Dim ck As Double = Double.Parse(gridPhieuHang.GetRowCellValue(index, colCK).ToString)
        Dim tienChiecKhau As Double = ck * 0.01 * ThanhTien
        gridPhieuHang.SetRowCellValue(index, colChietKhau, tienChiecKhau)
        Dim tongtien As Double = ThanhTien - tienChiecKhau
        gridPhieuHang.SetRowCellValue(index, colThanhToan, tongtien)
        tinhTongDonHang()
    End Sub

    Dim rowindex As Integer = 0
    Private Sub gridPhieuHang_CellValueChanging(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gridPhieuHang.CellValueChanging
        'Try
        If gridPhieuHang.GetRowCellValue(e.RowHandle, colMaHang) Is Nothing And gridPhieuHang.GetRowCellValue(e.RowHandle, colTenHang) Is Nothing Then
            gridPhieuHang.AddNewRow()
        Else
            For i = 0 To dtChiTietHH.Rows.Count - 1
                If dtChiTietHH.Rows(i)("MaHang").ToString = e.Value.ToString Then
                    gridPhieuHang.ClearSelection()
                    gridPhieuHang.FocusedRowHandle = i
                    gridPhieuHang.FocusedColumn = colSoLuong
                    gridPhieuHang.SelectRow(i)
                    Return
                End If
            Next
        End If
        If e.Column.FieldName = colMaHang.FieldName Or e.Column.FieldName = colTenHang.FieldName Then

            Dim hh As HangHoaBanHangDTO = dsHangHoa.Find(Function(hanghoa As HangHoaBanHangDTO)
                                                             Return hanghoa.MaHangHoa = e.Value.ToString
                                                         End Function)
            gridPhieuHang.SetRowCellValue(e.RowHandle, colSoLuong, 1)
            gridPhieuHang.SetRowCellValue(e.RowHandle, colCK, chiecKhau)
            gridPhieuHang.SetRowCellValue(e.RowHandle, colDonGia, hh.GiaBanLe)

            tinhTien(e.RowHandle, hh)
            tinhTongDonHang()
            gridPhieuHang.CloseEditor()
            gridPhieuHang.UpdateCurrentRow()
            gridPhieuHang.AddNewRow()

        Else
            If e.Column.FieldName = colSoLuong.FieldName And Not gridPhieuHang.GetRowCellValue(e.RowHandle, colMaHang) Is Nothing Then
                Dim hh As HangHoaBanHangDTO = dsHangHoa.Find(Function(hanghoa As HangHoaBanHangDTO)
                                                                 Return hanghoa.MaHangHoa = gridPhieuHang.GetRowCellValue(e.RowHandle, colMaHang).ToString
                                                             End Function)
                gridPhieuHang.SetRowCellValue(e.RowHandle, colSoLuong, e.Value)
                tinhTien(e.RowHandle, hh)
                tinhTongDonHang()
                gridPhieuHang.CloseEditor()
                'gridPhieuHang.UpdateCurrentRow()
            Else
                If e.Column.FieldName = colCK.FieldName And Not gridPhieuHang.GetRowCellValue(e.RowHandle, colMaHang) Is Nothing Then
                    Dim hh As HangHoaBanHangDTO = dsHangHoa.Find(Function(hanghoa As HangHoaBanHangDTO)
                                                                     Return hanghoa.MaHangHoa = gridPhieuHang.GetRowCellValue(e.RowHandle, colMaHang).ToString
                                                                 End Function)
                    Dim ck As Double = Double.Parse(e.Value.ToString)
                    gridPhieuHang.SetRowCellValue(e.RowHandle, colCK, ck)

                    tinhTien(e.RowHandle, hh)
                    tinhTongDonHang()
                    gridPhieuHang.CloseEditor()
                    'gridPhieuHang.UpdateCurrentRow()
                End If
            End If
        End If
        tinhTongDonHang()
    End Sub

    Private Sub gridPhieuHang_ValidateRow(sender As Object, e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) Handles gridPhieuHang.ValidateRow
        Try
            Dim View As GridView = CType(sender, GridView)
            Dim maHangCol As GridColumn = View.Columns("MaHang")
            Dim maHang As String = View.GetRowCellValue(e.RowHandle, colMaHang).ToString
            Dim soluongCol As GridColumn = View.Columns("SoLuong")
            Dim sl As Integer = CType(View.GetRowCellValue(e.RowHandle, "SoLuong"), Integer)
            If maHang = "" Then
                e.Valid = False
                'Set errors with specific descriptions for the columns
                View.SetColumnError(maHangCol, "Mã hàng không được để trống")
            End If
            Dim maHH As String = gridPhieuHang.GetRowCellValue(e.RowHandle, colMaHang).ToString
            If maHH <> "" Then
                Dim slton As Integer = dsHangHoa.Find(Function(hanghoa As HangHoaBanHangDTO)
                                                          Return hanghoa.MaHangHoa = maHH
                                                      End Function).SoLuongTon
                If sl > slton Then
                    e.Valid = False
                    View.SetColumnError(soluongCol, "Số lượng tồn chỉ còn lại " + slton.ToString + " sản phẩm")
                End If
            End If


        Catch ex As Exception

        End Try


    End Sub

    Private Sub gridPhieuHang_InvalidRowException(sender As Object, e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) Handles gridPhieuHang.InvalidRowException
        e.ExceptionMode = ExceptionMode.NoAction
    End Sub

    Private Sub cEditCkPT_EditValueChanged(sender As Object, e As EventArgs) Handles cEditCkPT.EditValueChanged, cEditVATPT.EditValueChanged, cEditVAT.EditValueChanged, cEditTongTien.EditValueChanged, cEditChietKhau.EditValueChanged
        If _load = True Then
            tinhTongDonHang()
        End If

    End Sub

    Private Sub tinhTongDonHang()
        If Double.Parse(colThanhToan.SummaryItem.SummaryValue.ToString) <> 0 Then
            tienChiecKhau = Double.Parse(cEditCkPT.EditValue.ToString) * Double.Parse(colThanhToan.SummaryItem.SummaryValue.ToString) * 0.01
            cEditChietKhau.EditValue = tienChiecKhau
            tienThueVat = (Double.Parse(colThanhToan.SummaryItem.SummaryValue.ToString)) * Double.Parse(cEditVATPT.EditValue.ToString) * 0.01
            cEditVAT.EditValue = tienThueVat
            cEditTongTien.EditValue = Double.Parse(colThanhToan.SummaryItem.SummaryValue.ToString) + tienThueVat - tienChiecKhau
        Else
            cEditTongTien.EditValue = gridPhieuHang.GetRowCellValue(0, colThanhToan)
        End If
    End Sub


    Private Sub btnTaoMoi_ItemClick(sender As Object, e As ItemClickEventArgs) Handles btnTaoMoi.ItemClick
        loadMaPhieu()
        dtChiTietHH.Rows.Clear()
        grdCtrlPhieuBan.DataSource = dtChiTietHH
        gridPhieuHang.AddNewRow()
    End Sub

    Private Sub btnLuuThem_ItemClick(sender As Object, e As ItemClickEventArgs) Handles btnLuuThem.ItemClick

        If pq.Them = True Then
            tinhTongDonHang()
            gridPhieuHang.CloseEditor()
            gridPhieuHang.UpdateCurrentRow()
            Dim tongDH As Double = 0
            If dtChiTietHH.Rows.Count > 1 Then
                dtChiTietHH.Rows.RemoveAt(dtChiTietHH.Rows.Count - 1)
                dsChiTiet = New BindingList(Of ChiTietBanHangDTO)
                For Each row As DataRow In dtChiTietHH.Rows
                    Dim ct As New ChiTietBanHangDTO
                    ct.MaHang = row("MaHang").ToString
                    ct.MaKho = row("MaKho").ToString
                    ct.SoLuong = Integer.Parse(row("SoLuong").ToString)
                    ct.DonGia = Double.Parse(row("DonGia").ToString)
                    ct.ThanhTien = Double.Parse(row("ThanhTien").ToString)
                    ct.ChietKhau = Double.Parse(row("ChietKhau").ToString)
                    ct.ThanhToan = Double.Parse(row("ThanhToan").ToString)
                    tongDH += ct.ThanhToan
                    dsChiTiet.Add(ct)
                Next
                phieuBH = New PBanHangDTO
                phieuBH.MaKhachHang = cbbMaKh.EditValue.ToString
                phieuBH.Ngay = Date.Parse(dateLapPhieu.EditValue.ToString)
                phieuBH.MaNhanVien = cbbNhanVienLap.EditValue.ToString
                phieuBH.NgayGiao = Date.Parse(dateGiao.EditValue.ToString)
                phieuBH.MaHinhThuc = Integer.Parse(cbbThanhToan.EditValue.ToString)
                phieuBH.MaDieuKhoan = Integer.Parse(cbDieuKienTT.EditValue.ToString)
                phieuBH.HanThanhToan = Date.Parse(dateThanhToan.EditValue.ToString)
                phieuBH.ChietKhau = Double.Parse(cEditCkPT.EditValue.ToString)
                phieuBH.VAT = Double.Parse(cEditVATPT.EditValue.ToString)
                phieuBH.TongDonHang = tongDH
                phieuBH.GhiChu = txtGhiChu.Text
                maDH = PBanHangBUS.themDonHang(phieuBH)
                Dim kq As Integer = PBanHangBUS.themChiTietDH(dsChiTiet, maDH)
                If kq = dsChiTiet.Count Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Thêm đơn hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    loadDSKH()
                    loadMaPhieu()
                    loadDSNhanVien()
                    loadDieuKhoanTT()
                    LoadHinhThuc()
                    loadThongTinNgay()
                    loadDSHangHoa()
                    loadDSKhoHang()
                    addDatasourceDataGrid()
                    dtChiTietHH.Rows.Clear()
                    dsChiTiet.Clear()
                    grdCtrlPhieuBan.DataSource = dtChiTietHH
                    gridPhieuHang.AddNewRow()
                    HeThongBUS.themNhatKy(frmMain.maND, "Bán Hàng", "Thêm đơn hàng", frmMain.maND + " thêm đơn hàng " + maPhieu)

                    If pq.Them = False Then
                        DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền in", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    Else
                        If (DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muôn in phiếu xuất hàng", "Thông Báo", _
              MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                            xuatPhieu(maDH)
                        End If


                    End If
                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Thêm đơn không hàng thành công", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If

            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Bạn chưa thêm đơn hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền thêm", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If
    End Sub

    Private Sub gridPhieuHang_CellValueChanged(sender As Object, e As CellValueChangedEventArgs) Handles gridPhieuHang.CellValueChanged
        'For i = 0 To gridPhieuHang.RowCount - 2
        '    For j = i + 1 To gridPhieuHang.RowCount - 1
        '        If gridPhieuHang.GetRowCellValue(i, colMaHang).ToString = gridPhieuHang.GetRowCellValue(j, colMaHang).ToString Then
        '            MessageBox.Show(i.ToString + "  " + gridPhieuHang.GetRowCellValue(i, colMaHang).ToString)
        '        End If
        '    Next
        'Next

    End Sub

    Private Sub grdCtrlPhieuBan_KeyDown(sender As Object, e As KeyEventArgs) Handles grdCtrlPhieuBan.KeyDown
        If (e.KeyCode = Keys.Delete) Then
            If (DevExpress.XtraEditors.XtraMessageBox.Show("Delete row?", "Confirmation", _
              MessageBoxButtons.YesNo) <> DialogResult.Yes) Then Return
            dtChiTietHH.Rows.RemoveAt(gridPhieuHang.FocusedRowHandle)
            grdCtrlPhieuBan.DataSource = dtChiTietHH
            tinhTongDonHang()
        End If
    End Sub

    Private Sub BarButtonItem5_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItem5.ItemClick
        grdCtrlPhieuBan.DataSource = dtChiTietHH
        tinhTongDonHang()
    End Sub

    Private Sub gridPhieuHang_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs) Handles gridPhieuHang.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        If ckbIn.Checked = True Then
            If pq.Them = False Then
                DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền in", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else

                If pq.Them = True Then
                    SplashScreenManager2.ShowWaitForm()
                    tinhTongDonHang()
                    gridPhieuHang.CloseEditor()
                    gridPhieuHang.UpdateCurrentRow()
                    If dtChiTietHH.Rows.Count > 1 Then
                        dtChiTietHH.Rows.RemoveAt(dtChiTietHH.Rows.Count - 1)
                        dsChiTiet = New BindingList(Of ChiTietBanHangDTO)
                        For Each row As DataRow In dtChiTietHH.Rows
                            Dim ct As New ChiTietBanHangDTO
                            ct.MaHang = row("MaHang").ToString
                            ct.MaKho = row("MaKho").ToString
                            ct.SoLuong = Integer.Parse(row("SoLuong").ToString)
                            ct.DonGia = Double.Parse(row("DonGia").ToString)
                            ct.ThanhTien = Double.Parse(row("ThanhTien").ToString)
                            ct.ChietKhau = Double.Parse(row("ChietKhau").ToString)
                            ct.ThanhToan = Double.Parse(row("ThanhToan").ToString)
                            dsChiTiet.Add(ct)
                        Next
                        phieuBH = New PBanHangDTO
                        phieuBH.MaKhachHang = cbbMaKh.EditValue.ToString
                        phieuBH.Ngay = Date.Parse(dateLapPhieu.EditValue.ToString)
                        phieuBH.MaNhanVien = cbbNhanVienLap.EditValue.ToString
                        phieuBH.NgayGiao = Date.Parse(dateGiao.EditValue.ToString)
                        phieuBH.MaHinhThuc = Integer.Parse(cbbThanhToan.EditValue.ToString)
                        phieuBH.MaDieuKhoan = Integer.Parse(cbDieuKienTT.EditValue.ToString)
                        phieuBH.HanThanhToan = Date.Parse(dateThanhToan.EditValue.ToString)
                        phieuBH.ChietKhau = Integer.Parse(cEditCkPT.EditValue.ToString)
                        phieuBH.VAT = Integer.Parse(cEditVATPT.EditValue.ToString)
                        phieuBH.TongDonHang = Double.Parse(cEditTongTien.EditValue.ToString)
                        phieuBH.GhiChu = txtGhiChu.Text
                        maDH = PBanHangBUS.themDonHang(phieuBH)
                        Dim kq As Integer = PBanHangBUS.themChiTietDH(dsChiTiet, maDH)
                        If kq = dsChiTiet.Count Then
                            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm đơn hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            loadDSKH()
                            loadMaPhieu()
                            loadDSNhanVien()
                            loadDieuKhoanTT()
                            LoadHinhThuc()
                            loadThongTinNgay()
                            loadDSHangHoa()
                            loadDSKhoHang()
                            addDatasourceDataGrid()
                            dtChiTietHH.Rows.Clear()
                            dsChiTiet.Clear()
                            grdCtrlPhieuBan.DataSource = dtChiTietHH
                            gridPhieuHang.AddNewRow()
                            HeThongBUS.themNhatKy(frmMain.maND, "Bán Hàng", "Thêm đơn hàng", frmMain.maND + " thêm đơn hàng " + maPhieu)
                            If pq.Them = False Then
                                DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền in", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                            Else
                              xuatPhieu(maDH)
                            End If
                        Else
                            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm đơn không hàng thành công", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If

                    Else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Bạn chưa thêm đơn hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                    SplashScreenManager2.CloseWaitForm()
                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền thêm", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                End If
            End If
        End If
    End Sub

    Private Sub xuatPhieu(maDH As String)
        Dim dt As New DataTable()
        dt = BaoCaoBUS.layPhieuBanHang(maDH)
        Dim rep As New PhieuXuatHang
        rep.BeginUpdate()
        rep.DataSource = dt
        rep.EndUpdate()
        rep.CreateDocument()

        Dim form As New PrintPreviewFormEx()
        form.Text = "Xem trước khi in"
        form.PrintingSystem = rep.PrintingSystem
        form.ShowDialog()
    End Sub

End Class
